Objects and classesΒΆ
Although Lua doesn't define classes in the language, it provides enough mechanisms to allow implementing classes. Guerilla implements its own class and object system.
Classes
To define a class, use class.
class ("MyClass")
To define the class constructor, define the construct method.
function MyClass:construct ()
[...]
end
The ':' notation is a shortcut for first argument being self. The following definition is syntaxically equivalent to the previous:
function MyClass.construct (self)
[...]
end
To derive a class from another/other class(es), add the names in order to the class call.
class ("MyClass", "Node", "AnotherClass")
Define a method with the same syntax as the constructor.
function MyClass:mymethod ()
[...]
end
Objects
To create an object, call the class itself.
myObject = MyClass ()The class constructor can have arguments, which are passed from the class calling.
function MyClass:construct (arg1, arg2)
[...]
end
myObject = MyClass ("hip", "hop")
To call an object method, call the method with the ':' operator.
myObject:mymethod ()